/*
 * @Description: 全宗上下文
 * @Author: Rfan
 * @Date: 2022-04-08 09:07:14
 * @LastEditTime: 2022-04-18 18:53:52
 */

import { createContext, useContext, useReducer } from 'react';
import { AddUserDialogReducer } from '../reducers/AddUserDialogReducer';
import { SET_SELECT_DEPARTMENTS, SET_SELECT_USERS } from '../constants';

const initialState = {
  // 选中的部门列表
  selectDepartments: [],
  // 选中的用户列表
  selectUsers: []
};

const AddUserDialogContext = createContext();

const AddUserDialogContextProvider = (props) => {
  const [state, dispatch] = useReducer(AddUserDialogReducer, initialState);

  const actions = {
    /**
     * @description: 设置选中的部门
     */
    setSelectDepartments(list) {
      dispatch({ type: SET_SELECT_DEPARTMENTS, list });
    },

    /**
     * @description: 设置选中的用户
     */
    setSelectUsers(list) {
      dispatch({ type: SET_SELECT_USERS, list });
    }
  };

  return (
    <AddUserDialogContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </AddUserDialogContext.Provider>
  );
};

export const useAddUserDialogContext = () => {
  return useContext(AddUserDialogContext);
};

export { AddUserDialogContext, AddUserDialogContextProvider };
